//
//  ComposeViewController.swift
//  swift_learning
//
//  Created by arons on 2017/2/3.
//  Copyright © 2017年 arons. All rights reserved.
//

import UIKit

class ComposeViewController: UIViewController {
    
    
    // MARK: - ......::::::: View :::::::......
    @IBOutlet weak var compostTextView: ComposeTextView!
    @IBOutlet weak var toolBar: UIToolbar!
    
    // MARK: - ......::::::: Constraint :::::::......
    @IBOutlet weak var toolBarBottomCons: NSLayoutConstraint!
    
    // MARK: - ......::::::: lazyload :::::::......
    private lazy var titleView : ComposeTitleView = ComposeTitleView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置导航栏
        setupNavigationBar()
        
        // 设置通知
        NotificationCenter.default.addObserver(self, selector: #selector(ComposeViewController.keyboardWillChangeFrame(not:)), name: .UIKeyboardWillChangeFrame, object: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - ......::::::: 初始化 :::::::......
    
    /// 设置导航栏
    private func setupNavigationBar() {
        // 1.设置左右的item
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(ComposeViewController.closeItemClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: .plain, target: self, action: #selector(ComposeViewController.sendItemClick))
        navigationItem.rightBarButtonItem?.isEnabled = false
        
        // 2.设置标题
        titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
        navigationItem.titleView = titleView
    }
    
    
    // MARK: - ......::::::: ui action :::::::......
    func closeItemClick() {
        dismiss(animated: true, completion: nil)
    }
    
    @objc private func sendItemClick() {
        
    }
    
    
    // MARK: - ......::::::: notification :::::::......
    
    func keyboardWillChangeFrame(not : Notification) {
        print("not\(not)")
        let keyboardRect = not.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect
        let y : CGFloat = keyboardRect?.origin.y ?? 0
        let duration : TimeInterval = not.userInfo?["UIKeyboardAnimationDurationUserInfoKey"] as! TimeInterval? ?? 0
        
        // 设置Toolbar的约束
        UIView.animate(withDuration: duration, animations: { [weak self] in
            self?.toolBarBottomCons.constant = SCREEN_HEIGHT() - y
        }, completion: nil)
    }
    
}
